﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EstateMan.Domain;
using FizzWare.NBuilder;

namespace EstateMan.DataAccess
{
    public class EstateManContext : DbContext
    {
        public EstateManContext()
            : base("name=DefaultConnection")
        {
            Configuration.LazyLoadingEnabled = true;
            Configuration.ProxyCreationEnabled = false;
        }

        public DbSet<Property> Properties { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Configurations.Add(new PropertyConfiguration());
        }
    }

    //public interface IDbContext : IDisposable
    //{
    //    DbSet<T> Set<T>() where T : class;

    //    DbEntityEntry<T> Entry<T>(T entity) where T : class;

    //    int SaveChanges();

    //}


    public class PropertyConfiguration : EntityTypeConfiguration<Property>
    {
        public PropertyConfiguration()
        {
            Property(p => p.Name).HasMaxLength(150);
            Property(p => p.Name).IsRequired();
            Property(p => p.AddressId).IsRequired();
            Property(p => p.AgentId).IsRequired();


            HasRequired(pt => pt.SubPropertyType)
                .WithMany()
                .HasForeignKey(p => p.SubPropertyTypeId)
                .WillCascadeOnDelete(false);

            HasRequired(pt => pt.PropertyType)
                .WithMany()
                .HasForeignKey(p => p.PropertyTypeId);

            Property(p => p.RowVersion).IsRowVersion();
            Property(p => p.AddressId).IsConcurrencyToken();

        }
    }

}
